<?php
// Library File
include 'lib/library.php';

$title = 'Search';

$found = false;

/**
 * Variables
 */

$item = mysql_real_escape_string($_GET['item']);
if(empty($item)){
	unset($item);
}
$user = mysql_real_escape_string($_GET['user']);
if(empty($user)){
	unset($user);
}

// Search defaults to search for item for NPO members
if(!isset($item) && $m_member_type == 'NPO'){

	// Set subtitle
	if(!$found){
		$subtitle = 'Search for Items';
	}	else{
		$subtitle = 'Items Found';
	}
	
	if(!$found){
		$content .= '
			<div class="form">
				
		';
				include $rel_addr.'/handle/search_handle.php';
		$content .= '
				<form action="'.$addr.this_page().'" method="POST">
				<div>
					<P>
					<input type="text" size="20" id="search" name="search" /> <input type="submit" name="submit" value="Search" />
					</P>
				</div>
					
				<br /><br /><br />
					<div>
					<P>
					<label for="tags">Search Tags: </label><br />
					<textarea cols="30" rows="2" id="tags" name="tags"></textarea>
					</P>
				</div>
				<br />
				</form>
			</div>
			
		';
	}

// An item is being looked at by an NPO member
}else if(isset($item) && $m_member_type == 'NPO'){

	$query = mysql_query("SELECT * FROM items WHERE item_id='$item'");
	$item_exists = mysql_num_rows($query);
	
	$content .= '
		<div class="itembox">
	';
	
	// The item_id exists
	if($item_exists){
		$item_array = mysql_fetch_assoc($query);
		
		// get name of user that donated item
		$query = mysql_query("SELECT name FROM users WHERE user_id='{$item_array['user_id']}'");
		$user_array = mysql_fetch_assoc($query);
		$name = $user_array['name'];
		
		// Get tag_ids related to item
		$tag_ids;
		$query = mysql_query("SELECT tag_id FROM tagmap WHERE item_id='$item'");
		while($tag_ids_array = mysql_fetch_array($query)){
			$tag_ids[] = $tag_ids_array['tag_id'];
		}
		
		// Get names of the tag_ids related to the item
		$tag_names;
		$tag_ids_count = count($tag_ids);
		for($x=0;$x < $tag_ids_count;$x++){
			$query = mysql_query("SELECT name FROM tags WHERE tag_id='{$tag_ids["$x"]}'");
			$tag_array = mysql_fetch_assoc($query);
			$tag_names[] = $tag_array['name'];
		}
		
		$content .= '
			<div class="image">
				<img src="'.$addr.'/images/'.$item_array['img_src'].'" alt="'.$item_array['img_src'].'" />
			</div>
			
			<div class="data name">
				<P>'.stripslashes($item_array['name']).'</P>
			</div>
			
			<div class="data donor">
				<P>Donated By: <a href="'.$addr.'/profile.php?id='.$item_array['user_id'].'">'.$name.'</a></P>
			</div>
			
			<div class="data description">
				<P>'.nl2br(stripslashes($item_array['description'])).'</P>
			</div>
			
			<div class="tags">
				<P><img src="'.$addr.'/imgs/tag.png" />Tags: 
		';
				$tag_names_count = count($tag_names);
				for($x=0;$x < $tag_names_count;$x++){
					$content .= ''.$tag_names["$x"].'';
					if($x < $tag_names_count - 1){
						$content .= ', ';
					}
				}
			
		$content .= '
				</P>
			</div>
			
			<div class="contact">
				<P><a href="'.$addr.'/messages.php?mode=compose&to='.$name.'&subject=About the '.stripslashes($item_array['name']).'">Contact</a></P>
			</div>
		';
		
	// The item_id doesn't exist
	}else if(!$item_exists){
		$content .= '
			<P> That item does not exist. </P>
		';
		refresh();
	}
	
	$content .= '
		</div>
	';

// Search page defaults to search user
// for non NPO members
}else if(!isset($user) && $m_member_type != 'NPO'){

	// Set subtitle
	$subtitle = 'Search for Users';

	$content .= '
		<P>A user search will be implemented in the future.</P>
		';
}

if((isset($_POST['submit']) && $found) || isset($item) || isset($user)){

	$side = '
		<ul>
		<li><a href="search.php">New Search</a></li>
		</ul>
		';
}else{
	$side = '
	
		';
}
/*
**   P U T    V A R S
**    O N    P A G E
*/
	head($title,$style,$scripts);
	body($header,$personal,$subtitle,$content,$navigation,$side);
	foot($footer,$dbc);
	
?>
